package com.imooc.bilibili.rabbit;

import com.alibaba.fastjson.JSONObject;
import com.imooc.bilibili.domain.UserMoment;
import com.imooc.bilibili.service.UserMomentsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * ProjectName imooc-bilibili
 *
 * @author xieyucan
 * <br>CreateDate 2022/10/24 11:06
 */
@Slf4j
@Service(value = "rabbitSendMessage")
public class RabbitSendMessage {

    @Resource
    private UserMomentsService userMomentsService;

    @Resource
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(UserMoment userMoment)
    {
        String toUserMoment = JSONObject.toJSONString(userMoment);
        Message message = new Message(toUserMoment.getBytes());
        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(String.valueOf(userMoment.getId()));
        rabbitTemplate.send(
                "exchange.my.message",
                "key.my",
                message,
                correlationData
        );
    }

    /**
     * 发送弹幕信息
     * @param message
     */
    public void sendDanMuMessage(Message message)
    {
        rabbitTemplate.send(
                "exchange.my.danmu",
                "key.danmu",
                message,
                null
        );
    }
}
